<template>
    <div class="publicpageview">
        <div class="publicformview">
            <el-form :inline="true" :model="listQuery" size="mini" ref="searchForm" label-width="100px">
                <el-row :gutter="20">

                    <el-col :span="4">
                        <el-form-item label="管理单位：" prop="districtId">
                            <el-select v-model="listQuery.districtId" clearable @change="handleQuchange">
                                <el-option v-for="item in areaList" :label="item.dname" :value="item.did"
                                    :key="item.did"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="4">
                        <el-form-item label="管理部：" prop="houseAdminId">
                            <el-select v-model="listQuery.houseAdminId" clearable>
                                <el-option v-for="item in houseAdminList" :label="item.aname" :value="item.aid"
                                    :key="item.aid"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <!-- <el-col :span="4">
                        <el-form-item label="提报编号：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="4">
                        <el-form-item label="所编号：" prop="keyword">
                            <el-input v-model="listQuery.keyword"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="所地址：" prop="blockAddress">
                            <el-input v-model="listQuery.blockAddress"></el-input>
                        </el-form-item>
                    </el-col>


                    <el-col :span="4">
                        <el-form-item label="承租人：" prop="renter">
                            <el-input v-model="listQuery.renter"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="维修单号：" prop="repairOrderNumber">
                            <el-input v-model="listQuery.repairOrderNumber"></el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <div style="padding-left: 90px;">
                            <el-button size="mini" type="primary" @click="handleSearch">查询</el-button>
                            <el-button size="mini" @click="resetForm('searchForm')">重置</el-button>
                        </div>
                    </el-col>
                </el-row>
            </el-form>
        </div>

        <div class="publitableview">

            <el-table :data="list" border fit highlight-current-row size="mini">
                <el-table-column align="center" label="区房屋管理单位" prop="districtName" min-width="200">
                </el-table-column>
                <el-table-column align="center" label="管理部（分中心）" prop="houseAdminName" min-width="150">
                </el-table-column>
                <el-table-column align="center" label="管片代码" prop="blockGroupName">
                </el-table-column>
                <el-table-column align="center" label="所编号" prop="blockId">
                </el-table-column>
                <el-table-column align="center" label="所地址" prop="blockAddress">
                </el-table-column>
                <el-table-column align="center" label="维修地址" prop="repairAddress">
                </el-table-column>
                <el-table-column align="center" label="维修工单" prop="repairOrderNumber">
                </el-table-column>
                <el-table-column align="center" label="承租人" prop="renter">
                </el-table-column>
                <el-table-column align="center" label="承租人电话" prop="renterPhone" min-width="130">
                </el-table-column>
                <el-table-column align="center" label="合同号" prop="contractNo">
                </el-table-column>
                <el-table-column align="center" label="维修日期" prop="repairDate">
                    <template slot-scope="scope">
                        {{getTime(scope.row.repairDate)}}
                    </template>
                </el-table-column>
                <el-table-column align="center" label="维修部门" prop="repairLocation">
                </el-table-column>
                <el-table-column align="center" label="维修项目" prop="repairItems">
                </el-table-column>
                <el-table-column align="center" label="维修单位" prop="maintenanceOrganization">
                </el-table-column>
                <el-table-column align="center" label="维修单位电话" prop="maintenanceOrganizationPhone" min-width="150">
                </el-table-column>
                <el-table-column align="center" label="完工情况" prop="completionStatus">
                </el-table-column>
                <el-table-column align="center" label="备注" prop="note">
                </el-table-column>
                <el-table-column align="center" label="操作" width="200" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleAddUser('add', scope.row)">回访</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row, 'update')">修改</el-button>
                        <el-button type="text" size="mini" @click="handleUpdate(scope.row,'details')">详情</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="publicpaginationview">
                <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                    :current-page="listQuery.pageNum" background :page-sizes="[10, 20, 50, 100]"
                    :page-size="listQuery.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"
                    small></el-pagination>
            </div>
        </div>

        <el-dialog :title="isAdd == 'add' ? '住户回访' : isAdd == 'update' ? '住户回访修改' : '住户回访详情'"
            :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form :model="formData" ref="dataForm" class="demo-ruleForm" label-position="right" label-width="120px"
                size="mini">
                <el-form-item label="综合评价：" prop="evaluation">
                    <el-input v-model="formData.evaluation"></el-input>
                </el-form-item>
                <el-form-item label="备注：" prop="note">
                    <el-input v-model="formData.note"></el-input>
                </el-form-item>
            </el-form>

            <div class="huifangneirong">回访内容 <span @click="handleAddHuifang"
                    style="font-size: 12px;cursor: pointer;padding-left: 20px;"><i
                        class="el-icon-circle-plus-outline"></i>
                    新增回访内容</span>
            </div>

            <el-table border :data="hfData" size="mini">
                <el-table-column align="center" label="评价项目" prop="item" min-width="200">
                </el-table-column>
                <el-table-column align="center" label="评价结果登记" prop="result" min-width="150">
                </el-table-column>
                <el-table-column align="center" label="评价描述" prop="description">
                </el-table-column>
                <el-table-column align="center" label="备注" prop="note">
                </el-table-column>
                <el-table-column align="center" label="操作">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleDeletehuifang(scope.$index)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose" size="medium" v-if="isAdd == 'details'">返
                    回</el-button>

                <el-button type="primary" @click="handleSave" size="medium" v-else>保 存</el-button>
            </span>
        </el-dialog>



        <el-dialog title="新增回访" :visible.sync="huifangadd" width="40%" :before-close="handlehuifangaddClose">
            <el-form :model="huifangData" ref="huifang" class="demo-ruleForm" label-position="right" label-width="120px"
                size="mini">
                <el-form-item label="评价项目：" prop="item">
                    <el-input v-model="huifangData.item"></el-input>
                </el-form-item>
                <el-form-item label="评价结果登记：" prop="result">
                    <el-input v-model="huifangData.result"></el-input>
                </el-form-item>
                <el-form-item label="评价描述：" prop="description">
                    <el-input v-model="huifangData.description"></el-input>
                </el-form-item>
                <el-form-item label="备注：" prop="note">
                    <el-input v-model="huifangData.note"></el-input>
                </el-form-item>
            </el-form>



            <span slot="footer" class="dialog-footer">


                <el-button type="primary" @click="handlehuifangsave" size="medium">保 存</el-button>
            </span>
        </el-dialog>




    </div>
</template>

<script>
import { listBrokenRepairOrder, createBrokenRepairOrderVisit, brokenRepairOrderVisitDetail, updateBrokenRepairOrderVisit, deleteBrokenRepairOrderVisit } from '@/api/repair'
import { getArea, getHouseAdmin, getBlockGroup, getBlockByGroup, upload } from '@/api/common'
import moment from 'moment';
export default {
    name: "AccommodationManagementIndex",

    data() {
        return {
            listQuery: {
                pageNum: 1,
                pageSize: 20,
                districtId: "",
                houseAdminId: "",
                blockId: "",
                blockAddress: "",
                renter: "",
                repairOrderNumber: ""
            },
            total: 0,
            list: [],
            hfData: [],
            dialogVisible: false,
            isAdd: 'add',
            formData: {
                evaluation: "",
                note: "",
                brokenRepairId: ""
            },
            areaList: [],
            houseAdminList: [],
            huifangadd: false,
            huifangData: {},
        };
    },

    computed: {
        getTime() {
            return (val) => {
                if (val) {
                    return moment(val).format("YYYY-MM-DD")
                } else {
                    return ""
                }
            }
        }
    },

    mounted() {
        this.getCommon();
        this.getList()
    },

    methods: {
        handleDeletehuifang(row) {
            this.hfData.splice(row, 1)
        },
        handlehuifangsave() {
            this.hfData.push({ ...this.huifangData });
            this.handlehuifangaddClose()
        },
        handleAddHuifang() {
            this.huifangadd = true;
        },

        handlehuifangaddClose() {
            this.resetForm('huifang');
            this.huifangadd = false;
        },

        handleSearch() {
            this.listQuery.pageNum = 1;
            this.getList();
        },

        getCommon() {
            getArea().then(res => {
                this.areaList = res.data
            })
        },

        handleQuchange(val) {
            this.listQuery.houseAdminId = null;
            if (val) {
                this.getHouseAdmin(val);

            } else {
                this.houseAdminList = [];
            }
        },

        getHouseAdmin(val) {
            getHouseAdmin({ DID: val, pageNum: 1, pageSize: 100000 }).then(res => {
                this.houseAdminList = res.data.list;
            })
        },

        handleSizeChange(val) {
            this.listQuery.pageNum = 1;
            this.listQuery.pageSize = val;
            this.getList();
        },
        handleCurrentChange(val) {
            this.listQuery.pageNum = val;
            this.getList();
        },
        getList() {
            listBrokenRepairOrder(this.listQuery).then((response) => {
                this.total = response.data.total;
                this.list = response.data.list;

            });
        },

        resetForm(val) {
            this.$refs[val].resetFields();
        },

        handleAddUser(type, row) {
            this.isAdd = 'add';
            this.dialogVisible = true;
            this.formData.brokenRepairId = row.id
        },

        handleClose() {
            this.resetForm("dataForm");
            this.dialogVisible = false;
            this.hfData = [];
        },

        handleSave() {
            const { hfData, formData } = this;
            if (this.isAdd == 'add') {
                createBrokenRepairOrderVisit({
                    brokenRepairOrderVisit: {
                        ...formData
                    },
                    visitItemList: [...hfData]
                }).then(res => {
                    this.handleClose();
                    this.getList();
                    this.$message.success("操作成功")
                })
            }

            if (this.isAdd == 'update') {
                updateBrokenRepairOrderVisit({
                    brokenRepairOrderVisit: {
                        ...formData
                    },
                    visitItemList: [...hfData]
                }).then(res => {
                    this.handleClose();
                    this.getList();
                    this.$message.success("操作成功")
                })
            }

            // updateBrokenRepairOrderVisit
        },



        handleUpdate(currentData, type) {
            this.isAdd = type;
            //   一定要在打开弹窗前 再赋值 不然resetFiles没用

            brokenRepairOrderVisitDetail(currentData.id).then(res => {
                this.dialogVisible = true;
                this.$nextTick(() => {
                    this.formData = { ...res.data.brokenRepairOrderVisit };
                    this.hfData = [...res.data.visitItemList]
                });
            })

        },



        handleDelete(id) {
            this.$confirm(
                "此操作将删除内容，请确认是否继续操作？",
                "删除提示",
                {
                    confirmButtonText: "保 存",
                    cancelButtonText: "取 消",
                    type: "warning",
                }
            )
                .then(() => {
                    deleteBrokenRepairOrderVisit(id).then(res => {
                        this.getList();
                        this.$message.success("操作成功")
                    })
                })
                .catch(() => { });
        }
    },
};
</script>

<style scoped>
.huifangneirong {

    font-family: SourceHanSansSC, SourceHanSansSC;
    font-weight: bold;
    font-size: 16px;
    color: #008C93;
    text-align: left;
    font-style: normal;
    text-transform: none;
    margin-bottom: 20px;
}
</style>